package com.deyuanyun.pic.pig.controller;

import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.deyuanyun.pic.common.cache.TempStoreDataSupport;
import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.util.ApplicationConfigUtil;
import com.deyuanyun.pic.common.util.PdfHelper;
import com.deyuanyun.pic.common.util.PdfUtils;
import com.deyuanyun.pic.common.util.UploadUtil;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.pig.domain.CleanPipeLineManagerVo;
import com.deyuanyun.pic.pig.mapper.PigTaskMapper;
import com.deyuanyun.pic.pig.service.CleanPipeLineHistoryService;
import com.deyuanyun.pic.service.pipe.PipeLineService;
/**
 * 清管历史管理控制类
 * @author tt
 *
 */
@Controller
@RequestMapping("/cleanTask/cleanPipeLineHistory")
public class CleanPipeLineHistoryController extends PiggingBaseController {
	static Logger logger=Logger.getLogger(CleanPipeLineHistoryController.class);
	@Resource
	private CleanPipeLineHistoryService cleanPipeLineHistoryService;
	@Resource(name="pipeLineServiceImpl")
	private PipeLineService pipeLineService;
	@Resource
	private PigTaskMapper pigTaskMapper;
	/**
	 * 跳转清管任务管理主页
	 * @return 
	 * 
	 * @return
	 */
	@RequestMapping("/getCleanPipeLineHistoryIndex.html")
	public ModelAndView getCleanPipeLineHistoryIndex() {
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("pipeLine", pipeLineService.getLine());
		map.put("code", pigTaskMapper.getcleanType());
	  return forword("/pig/cleanPipeLineHistory", map);
	}

	/**
	 * 加载列表数据
	 * @param vo
	 */
	@RequestMapping("/getDataList.json")
	@ResponseBody
	@SuppressWarnings("rawtypes")
	public void getDataList(CleanPipeLineManagerVo vo){
		PageJsonBean jsonBean = new PageJsonBean();
		List list = cleanPipeLineHistoryService.getCleanPipeLineHistoryIndex(vo);
		jsonBean.setList(list);
		AjaxSupport.sendSuccessText("", jsonBean);
	}
	
	/**
	 * 跳转详情页面
	 * @param taskId
	 * @return
	 */
	@RequestMapping("/getHistoryDetail.html")
	public ModelAndView getHistoryDetail(String taskId){
		Asserts.assertTrue(!StringUtils.isEmpty(taskId), "没有获取到对应的任务数据");
		Map<String,Object> map = new HashMap<String,Object>();
		map = cleanPipeLineHistoryService.getDetail(taskId,map);
		return forword("/pig/historyDetail", map);
	}
	
	/**
	 * 导出历史详情  pdf
	 * @param taskId
	 */
	@RequestMapping("/pdfExport.html")
	public void pdfExport(HttpServletRequest request,HttpServletResponse response,String taskId) throws Exception{
		String realPath=PdfHelper.getPath();
		
		String resourcePath = ApplicationConfigUtil.getConfigByName("intranetPath");
		Asserts.assertTrue(!StringUtils.isEmpty(resourcePath), "没有获取到导出路径");
		String imgPath = UploadUtil.SOURCE + UploadUtil.IMGURL;
		Map<String, Object> o=new HashMap<String, Object>();
		OutputStream os = null;
		try {
			if(null != taskId && !"".equals(taskId)){
				o = cleanPipeLineHistoryService.getDetailData(o,taskId,resourcePath,imgPath);
			}
			os= PdfUtils.generateToServletOutputStream(realPath, "pdfFile/templetPDF.ftl", imgPath, o, response);
			response.setContentType("application/pdf");
			response.setCharacterEncoding("UTF-8");
			response.setHeader("Content-disposition", "attachment; filename=piggingDetail.pdf" );
			byte[] buffer = new byte[1024];
			os.write(buffer);// 输出文件
			os.flush();
		} catch (Exception e) {
			logger.error("导出pdf文件出错",e);
		} finally{
			if(null != os){
				os.close();
			}
		}
	}
	/**
	 * 评价
	 * @param ids
	 * @return 
	 * @return
	 */
	@RequestMapping("/goEvluation.html")
	public ModelAndView goEvluation(String ids){
		Map<String,Object> map = new HashMap<String,Object>();
		if(!StringUtils.isEmpty(ids)){
			map = cleanPipeLineHistoryService.goEvluation(ids,map);
		}
		return forword("/pig/evaluate", map);
	}
	
	/**
	 * 导出预览页
	 * @param taskId
	 * @return 
	 * @return
	 */
	@RequestMapping("/viewPdf.html")
	public ModelAndView viewPdf(String taskId){
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("taskId", taskId);
		return forword("/pig/viewPdf", map);
	}
}
